package com.ax.base.common.controller;
import com.ax.base.common.domain.BaseChargeRules;
import com.ax.base.common.service.BaseChargeRulesService;
import com.ax.base.common.utils.AjaxRes;
import com.ax.base.common.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("sys/chargeRules")
public class BaseChargeRulesController {

    @Autowired
    BaseChargeRulesService baseChargeRulesService;

    /**
	 * 菜单页面的跳转
	 */
	@GetMapping("")
	String pharmacist(Model model) {
		return "common/basechargerules/basechargerules";
	}
	
	/**
     * 查询所有充值规则
     * @param ajaxRes
     * @param baseChargeRules
     * @return
     */
    @RequestMapping("queryChargeRules")
    @ResponseBody
    public List<BaseChargeRules> queryChargeRules(@RequestParam Map<String, Object> params){
        // PageUtils baseChargeRulesl=baseChargeRulesService.queryList(params);
    	   List<BaseChargeRules> baseChargeRules = baseChargeRulesService.queryList(params);
    	   return baseChargeRules;
    }
    
    /**
	 * 查看收费配置详情(新增)
	 * 
	 * @param  无
	 *           
	 * @return SUCC
	 */
	@GetMapping("/add")
	String add(Model model) {
		return "common/basechargerules/add";
	}
	
	/**
	 * 查看收费配置详情(修改)
	 * 
	 * @param Long id
	 *           
	 * @return BaseChargeRules baseChargeRules
	 */
	@GetMapping("/edit/{id}")
	String edit(Model model,@PathVariable("id") Long id) {
		BaseChargeRules baseChargeRules = new BaseChargeRules();
		baseChargeRules.setChargeRulesId(id);
		model.addAttribute("chargeRules",baseChargeRulesService.query(baseChargeRules));
		return "common/basechargerules/edit";
	}
    
    /**
     * 添加充值规则
     * @param ajaxRes
     * @param baseChargeRules
     * @return
     */
    @RequestMapping("insertChargeRule")
    @ResponseBody
    public AjaxRes addChargeRule(AjaxRes ajaxRes,BaseChargeRules baseChargeRules){
        try{
            this.baseChargeRulesService.addBaseChargeRules(baseChargeRules);
            ajaxRes.setSucceed(Const.SAVE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 更改充值规则
     * @param ajaxRes
     * @param baseChargeRules
     * @return
     */
    @RequestMapping("updateChargeRule")
    @ResponseBody
    public AjaxRes updateChargeRule(AjaxRes ajaxRes,BaseChargeRules baseChargeRules){
        try{
            this.baseChargeRulesService.updateBaseChargeRules(baseChargeRules);
            ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }


    /**
     * 删除充值规则
     * @param ajaxRes
     * @param chargeRulesId
     * @return
     */
    @RequestMapping("deleteChargeRules")
    @ResponseBody
    public AjaxRes deleteChargeRules(AjaxRes ajaxRes, @RequestParam("chargeRulesId[]")ArrayList<Long> chargeRulesId, @RequestParam("flag")Integer flag){
        try{
            Integer flags=baseChargeRulesService.deleteBaseChargeRules(chargeRulesId,flag);
            ajaxRes.setSucceed(flags,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setSucceed(e.getMessage());
        }
        return ajaxRes;
    }
}
